package com.example.jpetstoreclient.controller;

import com.example.jpetstoreclient.common.CommonResponse;
import com.example.jpetstoreclient.domain.Order;
import com.example.jpetstoreclient.service.OrderService;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //  获取订单
    @GetMapping("/{id}")
    public CommonResponse<List<Order>> getAll(@PathVariable("id") int id){
        return orderService.getAllOrders(id);
    }

    //  获取指定订单
    @GetMapping("/get/{id}")
    public CommonResponse<Order> getById(@PathVariable("id") int id){
        Order order = orderService.getById(id);
        if (order != null){
            return CommonResponse.createForSuccess("获取成功", order);
        }
        return CommonResponse.createForError("获取失败");
    }

    //  删除订单
    @DeleteMapping("/delete/{id}")
    public CommonResponse<Object> delete(@PathVariable("id") @NotBlank(message = "删除的订单不能为空") int id){
        return orderService.deleteOrder(id);
    }

    //  更新订单信息
    @PutMapping("/update")
    public CommonResponse<Order> update(@RequestBody Order order){
        if (orderService.updateById(order)){
            return CommonResponse.createForSuccessMessage("修改订单信息成功");
        }
        return CommonResponse.createForError("修改订单信息失败");
    }

    // 付款
    @PutMapping("/pay/{id}")
    public CommonResponse<Order> pay(@PathVariable("id") int id){
        return orderService.payOrder(id);
    }

    //  新增订单
    @PostMapping("/save")
    public CommonResponse<Object> save(@RequestBody Order order){
        return orderService.addOrder(order);
    }
}
